﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace ProductSystem.BLL.Common
{
    public static class PagerExtensions
    {
        /// < summary>    
        /// 分页Pager显示    
        /// < /summary>     
        /// < param name="html">< /param>    
        /// < param name="currentPageStr">标识当前页码的QueryStringKey< /param>     
        /// < param name="pageSize">每页显示< /param>    
        /// < param name="totalCount">总数据量< /param>    
        /// < returns>< /returns>   
        public static HtmlString Pager(this HtmlHelper html, string currentPageStr, int pageSize, int totalCount)
        {
            //css样式
            IDictionary<string, object> htmlattr = new Dictionary<string, object>();
            //htmlattr.Add("class", "test");
            IDictionary<string, object> htmlattrFirst = new Dictionary<string, object>();
            htmlattrFirst.Add("aria-label", "Previous");
            IDictionary<string, object> htmlattrLast = new Dictionary<string, object>();
            //htmlattrLast.Add("aria-label", "Next");




            var queryString = html.ViewContext.HttpContext.Request.QueryString;
            int currentPage = 1; //当前页    
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数    
            var dict = new System.Web.Routing.RouteValueDictionary(html.ViewContext.RouteData.Values);
            var output = new System.Text.StringBuilder();



            //VIEW返回路由，如Path文件名，适应能多层下分页
            string[] path = HttpContext.Current.Request.Path.Split('/');
            dict["action"] = path[path.Length - 1];

            if (queryString.Count > 0)
            {
                //与相应的QueryString绑定   
                foreach (string key in queryString.Keys)
                    if (queryString[key] != null && !string.IsNullOrEmpty(key))
                        dict[key] = queryString[key];
                int.TryParse(queryString[currentPageStr], out currentPage);
            }
            else
            {
                if (dict[currentPageStr] == null)
                {
                    currentPage = 1;
                }
                else
                {
                    //获取 ～/Page/{page number} 的页号参数  
                    int.TryParse(dict[currentPageStr].ToString(), out currentPage);
                }
            }
            if (currentPage <= 0)
            {
                currentPage = 1;
            }

            output.Append("<div class='data-pager-left'>");

            if (totalPages > 1)
            {
                output.Append("<ul>");
                if (currentPage != 1)
                {
                    //处理首页连接    
                    dict[currentPageStr] = 1;
                    output.Append("<li>");
                    output.Append(html.RouteLink("首页", dict, htmlattr));
                    output.Append("</li>");
                }
                if (currentPage > 1)
                {
                    //处理上一页的连接    
                    dict[currentPageStr] = currentPage - 1;
                    output.Append("<li>");
                    //string linkHtml = "<span aria-hidden='true'><img border='0' src='../Content/images/btn-pre.png'></span>";
                    MvcHtmlString preHtml = html.RouteLink("上一页", dict, htmlattrFirst);
                    //string preHtmlStr = preHtml.ToHtmlString().Replace("上一页", linkHtml);     //将a标签文本“上一页”替换为html
                    output.Append(preHtml);
                    output.Append("</li>");
                }
                //else
                //{
                //    output.Append("<li>");
                //    output.Append("<a href='#'></a>");
                //    output.Append("</li>");
                //}
                int currint = 5;
                for (int i = 0; i <= 10; i++)
                {
                    //一共最多显示10个页码，前面5个，后面5个    
                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
                        if (currint == i)
                        {
                            //当前页处理   
                            output.Append("<li>");
                            output.Append(string.Format("<a href='#' class='active'>{0}</a>", currentPage));
                            output.Append("</li>");
                        }
                        else
                        {
                            //一般页处理   
                            dict[currentPageStr] = currentPage + i - currint;
                            output.Append("<li>");
                            output.Append(html.RouteLink((currentPage + i - currint).ToString(), dict));
                            output.Append("</li>");
                        }
                }
                if (currentPage < totalPages)
                {
                    //处理下一页的链接   
                    dict[currentPageStr] = currentPage + 1;
                    output.Append("<li>");
                    //string linkHtml = "<span aria-hidden='true'><img border='0' src='../Content/images/btn-next.png'></span>";
                    MvcHtmlString nextHtml = html.RouteLink("下一页", dict, htmlattrLast);
                    //string nextHtmlStr = nextHtml.ToHtmlString().Replace("下一页", linkHtml);     //将a标签文本“下一页”替换为html
                    output.Append(nextHtml);
                    output.Append("</li>");
                }
                //else
                //{
                //    output.Append("<li>");
                //    output.Append("<a href='#'></a>");
                //    output.Append("</li>");
                //}
                if (currentPage != totalPages)
                {
                    dict[currentPageStr] = totalPages;
                    output.Append("<li>");
                    output.Append(html.RouteLink("末页", dict));
                    output.Append("</li>");
                }
                output.Append("</ul>");
                output.AppendFormat("<span class='data-pager-left-number'>{0}/{1}</span>", currentPage, totalPages);//这个统计加不加都行  
                output.Append("</div>");
            }
            return new HtmlString(output.ToString());
        }

        /// <summary>
        /// 每页大小
        /// </summary>
        /// <param name="html"></param>
        /// <param name="currentPageStr"></param>
        /// <param name="pageSizeStr"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static HtmlString PageSize(this HtmlHelper html, string currentPageStr, string pageSizeStr, int totalCount)
        {
            //css样式
            IDictionary<string, object> htmlattr = new Dictionary<string, object>();
            htmlattr.Add("class", "test");
            IDictionary<string, object> htmlattrAeach = new Dictionary<string, object>();
            //htmlattrAeach.Add("class", "btn btn-default");
            //htmlattrAeach.Add("role", "button");

            var queryString = html.ViewContext.HttpContext.Request.QueryString;
            int currentPage = 1; //当前页    
            int pageSize = 10;    //每页行数
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数    
            var dict = new System.Web.Routing.RouteValueDictionary(html.ViewContext.RouteData.Values);
            var output = new System.Text.StringBuilder();

            output.Append("<div class='data-pager-right'>");


            //VIEW返回路由，如Path文件名，适应能多层下分页
            string[] path = HttpContext.Current.Request.Path.Split('/');
            dict["action"] = path[path.Length - 1];

            if (queryString.Count > 0)
            {
                //与相应的QueryString绑定   
                foreach (string key in queryString.Keys)
                    if (queryString[key] != null && !string.IsNullOrEmpty(key))
                        dict[key] = queryString[key];
                int.TryParse(queryString[currentPageStr], out currentPage);
                int.TryParse(queryString[pageSizeStr], out pageSize);
            }
            else
            {
                if (dict[currentPageStr] == null)
                {
                    currentPage = 1;
                }
                else
                {
                    //获取 ～/Page/{page number} 的页号参数  
                    int.TryParse(dict[currentPageStr].ToString(), out currentPage);
                }

                if (dict[pageSizeStr] == null)
                {
                    pageSize = 10;
                }
                else
                {
                    //获取 ～/Page/{page number} 的每页行数
                    int.TryParse(dict[pageSizeStr].ToString(), out pageSize);
                }
            }
            if (pageSize <= 0)
            {
                pageSize = 10;
            }
            dict[currentPageStr] = 1;     //默认为第一页 

            if (HttpContext.Current.Request.Cookies["PageSize"] != null)
            {
                pageSize = Int32.Parse(HttpContext.Current.Request.Cookies["PageSize"].Value);
            }
            output.AppendFormat("{0}", "<span class='data-pager-right-number'>每页</span>");
            output.Append("<ul>");
            int[] sizeArr = new int[3] { 10, 20, 50 };
            for (int i = 0; i < sizeArr.Length; i++)
            {
                if (sizeArr[i] == pageSize)
                {
                    output.Append(string.Format("<li><a href='javascript:void(0)' class='active'>{0}</a></li>", pageSize));
                }
                else
                {
                    dict[pageSizeStr] = sizeArr[i];
                    output.Append("<li>" + html.RouteLink(sizeArr[i].ToString(), dict, htmlattrAeach) + "</li>");
                }
            }
            output.Append("</ul>");
            output.AppendFormat("{0}", "<span class='data-pager-right-number'>行</span>");
            output.Append("</div>");
            return new HtmlString(output.ToString());
        }


        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="html"></param>
        /// <param name="currentPageStr"></param>
        /// <param name="pageSizeStr"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static string Sort(this HtmlHelper html, string currentPageStr, string pageSizeStr, string sortFieldStr, int totalCount)
        {
            var queryString = html.ViewContext.HttpContext.Request.QueryString;
            int currentPage = 1; //当前页    
            int pageSize = 10;    //每页行数
            string sortField = "";   //排序字段
            var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数    
            var dict = new System.Web.Routing.RouteValueDictionary(html.ViewContext.RouteData.Values);
            var output = new System.Text.StringBuilder();
            //VIEW返回路由，如Path文件名，适应能多层下分页
            string[] path = HttpContext.Current.Request.Path.Split('/');
            dict["action"] = path[path.Length - 1];

            if (queryString.Count > 0)
            {
                //与相应的QueryString绑定   
                foreach (string key in queryString.Keys)
                    if (queryString[key] != null && !string.IsNullOrEmpty(key))
                        dict[key] = queryString[key];
                int.TryParse(queryString[currentPageStr], out currentPage);
                int.TryParse(queryString[pageSizeStr], out pageSize);
                sortField = queryString[sortFieldStr];

            }
            else
            {
                if (dict[currentPageStr] == null)
                {
                    currentPage = 1;
                }
                else
                {
                    //获取 ～/Page/{page number} 的页号参数  
                    int.TryParse(dict[currentPageStr].ToString(), out currentPage);
                }

                if (dict[pageSizeStr] == null)
                {
                    pageSize = 10;
                }
                else
                {
                    //获取 ～/Page/{page number} 的每页行数
                    int.TryParse(dict[pageSizeStr].ToString(), out pageSize);
                }
                if (dict[sortFieldStr] == null)
                {
                    sortField = "";
                }
                else
                {
                    sortField = queryString[sortFieldStr];
                }
            }
            if (pageSize <= 0)
            {
                pageSize = 10;

            }
            if (sortField == "")
            {
                dict[sortFieldStr] = "Year";     //默认按年份排序
            }
            dict[currentPageStr] = 1;     //默认为第一页 

            output.Append(html.RouteLink("hidden", dict));



            return output.ToString();
        }


        /// <summary>
        /// 获取分页(支持地址路由)
        /// </summary>
        /// <param name="urlFormat">url格式，用{0}代替页码</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">总页数</param>
        /// <param name="recordCount">总记录数</param>
        /// <param name="mode">分页模式</param>
        /// <returns>html</returns>
        public static string PagerBar(this HtmlHelper helper, string urlFormat, int pageIndex, int pageSize, int recordCount, PageMode mode)
        {

            pageIndex = (pageIndex > 0 ? pageIndex : 1);

            pageSize = PagerHelpCustomer.GetPageSize();
            int pageCount = (recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1);
            if (pageCount > 0 && pageIndex > pageCount)
            {
                pageIndex = pageCount;
            }

            StringBuilder sb = new StringBuilder();

            string strSelect = string.Empty;
            strSelect = "<ul>";
            strSelect += "<li>";
            strSelect += "<select id=\"pageSize\" name=\"PageSize\"  class=\"pages_on\" onchange='_PagePostSubmit(\"" + urlFormat.Replace("{0}", "") + "\"+$(\"#inputPage\").val())' >";
            if (pageSize == 20)
                strSelect += "<option value=\"20\" selected>20</option>";
            else
                strSelect += "<option value=\"20\">20</option>";
            if (pageSize == 50)
                strSelect += "<option value=\"50\" selected>50</option>";
            else
                strSelect += "<option value=\"50\">50</option>";
            if (pageSize == 100)
                strSelect += "<option value=\"100\" selected>100</option>";
            else
                strSelect += "<option value=\"100\" >100</option>";
            strSelect += "</select>";
            strSelect += "</li>";

            //sb.AppendFormat("共{0}条", recordCount);
            //sb.Append("每页");
            sb.Append(strSelect);
            sb.Append("<li>");
            if (pageIndex == 1)
            {
                sb.Append("<a href='javascript:void(0)' >");
                sb.Append("<img src='/EntContent/images/next_left_button_01.gif' width=\"22\" height=\"22\" alt='' /><span class=\"tip\"></span></a>");
            }
            else
            {
                string url1 = string.Format(urlFormat.ToString(), 1);
                sb.AppendFormat("<a href='javascript:_PagePostSubmit (\"{0}\")' ><img src='/EntContent/images/next_left_button_01.gif' width=\"22\" height=\"22\" alt='' />{1}</a>", url1, "<span class=\"tip\"></span>");
            }

            if (pageIndex > 1)
            {
                string url1 = string.Format(urlFormat.ToString(), pageIndex - 1);
                sb.AppendFormat("<a href='javascript:_PagePostSubmit (\"{0}\")' ><img src='/EntContent/images/next_left_button_02.gif' width=\"22\" height=\"22\" alt='' />{1}", url1, "<span class=\"tip\"></span></a>");
            }
            else
            {
                sb.Append(" <a href='javascript:void(0)' ><img src='/EntContent/images/next_left_button_02.gif' width=\"22\" height=\"22\" alt='' /><span class=\"tip\"></span></a>");
            }
            sb.Append("</li>");
            string urlFor = urlFormat.Replace("{1}", "\"+$(\"#pageSize\").val()+\"");
            sb.Append("<li>");
            sb.Append("<input type='text' class='table_next' onfocus='$(this).select();' onblur='/^[0-9]+$/.test($(this).val())?$(\"#enter_page\").focus():$(this).val(1)' value=" + pageIndex + " id='inputPage'");
            sb.Append("onkeydown=\"EnterSubmit(event.keyCode||event.which)\";/>");
            sb.AppendFormat("/{0}页", pageCount);
            sb.Append("</li>");

            sb.Append("<li>");
            if (pageIndex < pageCount)
            {

                string url1 = string.Format(urlFormat.ToString(), pageIndex + 1);
                sb.AppendFormat("<a href='javascript:_PagePostSubmit (\"{0}\")' ><img src='/EntContent/images/right_left_button_02.gif' width=\"22\" height=\"22\" alt='' />{1}", url1, "<span class=\"tip\"></span></a>");

            }
            else
            {
                sb.Append(" <a href='javascript:void(0)' ><img src='/EntContent/images/right_left_button_02.gif' width=\"22\" height=\"22\" alt='' /><span class=\"tip\"></span></a>");
            }

            if (pageIndex == pageCount || (pageIndex == 1 && pageCount == 0))
            {
                sb.Append(" <a href='javascript:void(0)' ><img src='/EntContent/images/right_left_button_01.gif' width=\"22\" height=\"22\" alt='' /><span class=\"tip\"></span></a>");
            }
            else
            {

                string url1 = string.Format(urlFormat.ToString(), pageCount);
                sb.AppendFormat("<a href='javascript:_PagePostSubmit (\"{0}\")' ><img src='/EntContent/images/right_left_button_01.gif' width=\"22\" height=\"22\" alt='' />{1}", url1, "<span class=\"tip\"></span></a>");

            }
            sb.Append("</li>");
            sb.Append("</ul>");
            sb.Append("<span>");
            sb.AppendFormat("共{0}条", recordCount);
            sb.Append("</span>");


            sb.Append("<input type='hidden' id='hidPageSize' value='" + pageSize + "'>");

            sb.Append("<script>function EnterSubmit(e)");
            sb.Append("{");
            sb.Append("if(e==13|| e==32)");
            sb.Append("{");
            sb.Append("_PagePostSubmit(\"" + urlFor.Replace("{0}", "\"+$(\"#inputPage\").val()+\"") + "\")");
            sb.Append("}");
            sb.Append("}");
            sb.Append("</script>");

            return sb.ToString();
        }

        /// <summary>
        /// 获取数字分页
        /// </summary>
        private static string GetNumericPage(string url, int pageIndex, int pageSize, int recordCount, int pageCount)
        {
            int k = pageIndex / 10;
            int m = pageIndex % 10;
            StringBuilder sb = new StringBuilder();
            if (pageIndex / 10 == pageCount / 10)
            {
                if (m == 0)
                {
                    k--;
                    m = 10;
                }
                else
                    m = pageCount % 10;
            }
            else
                m = 10;
            for (int i = k * 10 + 1; i <= k * 10 + m; i++)
            {
                if (i == pageIndex)
                    sb.AppendFormat("<li class='core_box'>{0}</li>", i);
                else
                {
                    string url1 = string.Format(url.ToString(), i, pageSize);
                    sb.AppendFormat("<li><a href='javascript:_PagePostSubmit (\"{0}\")'>{1}</a></li>", url1, i);
                }
            }

            return sb.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="style"></param>
        /// <param name="urlFormat"></param>
        /// <param name="isForm"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static string PagerBar(this HtmlHelper helper, Hashtable style, string urlFormat, bool isForm, int pageIndex, int pageSize, int recordCount, PageMode mode)
        {
            pageIndex = (pageIndex > 0 ? pageIndex : 1);
            int pageCount = (recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1);
            if (pageCount > 0 && pageIndex > pageCount)
            {
                pageIndex = pageCount;
            }
            StringBuilder sb = new StringBuilder();
            if (isForm)
            {
                sb.AppendFormat("<ul>总共{0}条记录,共{1}页&nbsp;&nbsp;", recordCount, pageCount);
            }
            else
            {
                sb.AppendFormat("<ul>");
            }
            if (pageIndex == 1)
                sb.Append("<li><a href='javascript:;' class='" + style["homePage"] + "'>首页</a></li>");
            else
            {
                string url1 = string.Format(urlFormat.ToString(), 1);
                sb.AppendFormat("<li><a class='" + style["homePage"] + "' href='javascript:_PagePostSubmit (\"{0}\")'>{1}", url1, "首页</a></li>");
            }
            if (pageIndex > 1)
            {
                string url1 = string.Format(urlFormat.ToString(), pageIndex - 1);
                sb.AppendFormat("<li><a  class='" + style["prePage"] + "' href='javascript:_PagePostSubmit (\"{0}\")'>{1}", url1, "上一页</a></li>");
            }
            else
                sb.Append("<li><a href='javascript:;'class='" + style["nextPage"] + "'>上一页</a></li>");
            if (mode == PageMode.Numeric)
                sb.Append(GetNumericPage(urlFormat.ToString(), style, pageIndex, pageSize, recordCount, pageCount));
            if (pageIndex < pageCount)
            {
                string url1 = string.Format(urlFormat.ToString(), pageIndex + 1);
                sb.AppendFormat("<li><a  class='" + style["nextPage"] + "'href='javascript:_PagePostSubmit (\"{0}\")'>{1}", url1, "下一页</a></li>");
            }
            else
                sb.Append("<li ><a href='javascript:;' class='" + style["nextPage"] + "'>下一页</a></li>");

            if (pageIndex == pageCount || (pageIndex == 1 && pageCount == 0))
                sb.Append("<li ><a href='javascript:;'class='" + style["lastPage"] + "'>末页</a></li>");
            else
            {
                string url1 = string.Format(urlFormat.ToString(), pageCount);
                sb.AppendFormat("<li><a class='" + style["lastPage"] + "' href='javascript:_PagePostSubmit (\"{0}\")'>{1}", url1, "末页</a></li>");
            }
            if (isForm)
            {
                sb.Append("跳至<input type='text' class='" + style["text"] + "' onfocus='$(this).select();' onblur='/^[0-9]+$/.test($(this).val())?$(\"#enter_page\").focus():$(this).val(1)' value=" + pageIndex + " id='inputPage'/>页");
                sb.Append("<input type='button' class='" + style["button"] + "' onclick='_PagePostSubmit(\"" + urlFormat.Replace("{0}", "") + "\"+$(\"#inputPage\").val())' id='enter_page' value='跳转'/></li></ul>");
            }
            else
            {
                sb.Append("</ul>");
            }
            return sb.ToString();
        }

        /// <summary>
        /// 获取数字分页
        /// </summary>
        /// <param name="url"></param>
        /// <param name="style"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <param name="pageCount"></param>
        /// <returns></returns>
        private static string GetNumericPage(string url, Hashtable style, int pageIndex, int pageSize, int recordCount, int pageCount)
        {
            int k = pageIndex / 10;
            int m = pageIndex % 10;
            StringBuilder sb = new StringBuilder();
            if (pageIndex / 10 == pageCount / 10)
            {
                if (m == 0)
                {
                    k--;
                    m = 10;
                }
                else
                    m = pageCount % 10;
            }
            else
                m = 10;
            for (int i = k * 10 + 1; i <= k * 10 + m; i++)
            {
                if (i == pageIndex)
                    sb.AppendFormat("<li><a href='javascript:;' class='" + style["currentPage"] + "' >{0}</a></li>", i);
                else
                {
                    string url1 = string.Format(url.ToString(), i);
                    sb.AppendFormat("<li><a href='javascript:_PagePostSubmit (\"{0}\")'>{1}</a></li>", url1, i);
                }
            }

            return sb.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static HtmlString PagerBar(this HtmlHelper helper, int pageIndex, int pageSize, int recordCount, PageMode mode)
        {
            var dict = new System.Web.Routing.RouteValueDictionary(helper.ViewContext.RouteData.Values);
            string urlFormat = dict["action"].ToString().Replace("~/Views", "").Replace(".cshtml", "") + "?pageIndex={0}&PageSize={1}";
            string str = PagerBarSize(helper, urlFormat, pageIndex, pageSize, recordCount, mode);
            HtmlString htmlStr = new HtmlString(str);
            return htmlStr;
        }
        /// <summary>
        /// 获取分页(支持地址路由)
        /// </summary>
        /// <param name="urlFormat">url格式，用{0}代替页码,{1}每页显示条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">总页数</param>
        /// <param name="recordCount">总记录数</param>
        /// <param name="mode">分页模式</param>
        /// <returns>html</returns>
        public static string PagerBarSize(this HtmlHelper helper, string urlFormat, int pageIndex, int pageSize, int recordCount, PageMode mode)
        {

            pageIndex = (pageIndex > 0 ? pageIndex : 1);

            pageSize = PagerHelpCustomer.GetPageSize();
            int pageCount = (recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1);
            if (pageCount > 0 && pageIndex > pageCount)
            {
                pageIndex = pageCount;
            }

            StringBuilder sb = new StringBuilder();
            string newUrlFormat = urlFormat.Replace("{1}", "\"+$(\"#PageSize\").val()").Replace("{0}", "\"+$(\"#inputPage\").val()+\"");
            string strSelect = string.Empty;
            sb.Append("<ul>");
            sb.Append("<li>");
            strSelect = "<select id=\"PageSize\" name=\"PageSize\"  class=\"pages_on\" onchange='_PagePostSubmit(\"" + newUrlFormat + ")' >";
            if (pageSize == 10)
                strSelect += "<option value=\"10\" selected>10</option>";
            else
                strSelect += "<option value=\"10\">10</option>";
            if (pageSize == 20)
                strSelect += "<option value=\"20\" selected>20</option>";
            else
                strSelect += "<option value=\"20\">20</option>";
            if (pageSize == 50)
                strSelect += "<option value=\"50\" selected>50</option>";
            else
                strSelect += "<option value=\"50\">50</option>";
            if (pageSize == 100)
                strSelect += "<option value=\"100\" selected>100</option>";
            else
                strSelect += "<option value=\"100\" >100</option>";
            strSelect += "</select>";
            sb.Append(strSelect);
            sb.Append("</li>");
            sb.Append("<li>");
            if (pageIndex == 1)
            {
                sb.Append("<a href='javascript:void(0)'>");
                sb.Append("<img src='/EntContent/images/next_left_button_01.gif' width=\"22\" height=\"22\" alt='' /><span class=\"tip\"></span></a>");
            }
            else
            {
                string url1 = string.Format(urlFormat.ToString(), 1, pageSize);
                sb.AppendFormat("<a href='javascript:_PagePostSubmit (\"{0}\")' ><img src='/EntContent/images/next_left_button_01.gif' width=\"22\" height=\"22\" alt='' />{1}</a>", url1, "<span class=\"tip\"></span>");
            }

            if (pageIndex > 1)
            {
                string url1 = string.Format(urlFormat.ToString(), pageIndex - 1, pageSize);
                sb.AppendFormat("<a href='javascript:_PagePostSubmit (\"{0}\")' ><img src='/EntContent/images/next_left_button_02.gif' width=\"22\" height=\"22\" alt='' />{1}", url1, "<span class=\"tip\"></span></a>");
            }
            else
            {
                sb.Append(" <a href='javascript:void(0)' ><img src='/EntContent/images/next_left_button_02.gif' width=\"22\" height=\"22\" alt='' /><span class=\"tip\"></span></a>");
            }
            sb.Append("</li>");
            sb.Append("<li>");
            sb.Append("<input type='text' class='table_next' onfocus='$(this).select();' onblur='/^[0-9]+$/.test($(this).val())?$(\"#enter_page\").focus():$(this).val(1)' value=" + pageIndex + " id='inputPage'");
            sb.Append("onkeydown=\"EnterSubmit(event.keyCode||event.which)\"/>");
            sb.AppendFormat("/{0}页", pageCount);
            sb.Append("</li>");

            sb.Append("<li>");
            if (pageIndex < pageCount)
            {
                string url1 = string.Format(urlFormat.ToString(), pageIndex + 1, pageSize);
                sb.AppendFormat("<a href='javascript:_PagePostSubmit (\"{0}\")' ><img src='/EntContent/images/right_left_button_02.gif' width=\"22\" height=\"22\" alt='' />{1}", url1, "<span class=\"tip\"></span></a>");
            }
            else
                sb.Append(" <a href='javascript:void(0)' ><img src='/EntContent/images/right_left_button_02.gif' width=\"22\" height=\"22\" alt='' /><span class=\"tip\"></span></a>");

            if (pageIndex == pageCount || (pageIndex == 1 && pageCount == 0))
                sb.Append(" <a href='javascript:void(0)' ><img src='/EntContent/images/right_left_button_01.gif' width=\"22\" height=\"22\" alt='' /><span class=\"tip\"></span></a>");
            else
            {
                string url1 = string.Format(urlFormat.ToString(), pageCount, pageSize);
                sb.AppendFormat("<a href='javascript:_PagePostSubmit (\"{0}\")' ><img src='/EntContent/images/right_left_button_01.gif' width=\"22\" height=\"22\" alt='' />{1}", url1, "<span class=\"tip\"></span></a>");
            }
            sb.Append("</li>");
            sb.Append("</ul>");
            sb.Append("<span>");
            sb.AppendFormat("共{0}条", recordCount);
            sb.Append("</span>");
            sb.Append("<input type='hidden' id='hidPageSize' value='" + pageSize + "'>");

            sb.Append("<script>function EnterSubmit(e)");
            sb.Append("{");
            sb.Append("if(e==13|| e==32)");
            sb.Append("{");
            sb.Append("_PagePostSubmit(\"" + newUrlFormat + ")");
            sb.Append("}");
            sb.Append("}");
            sb.Append("</script>");

            return sb.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static HtmlString PagerBarPostion(this HtmlHelper helper, int pageIndex, int pageSize, int recordCount, PageMode mode)
        {
            var dict = new System.Web.Routing.RouteValueDictionary(helper.ViewContext.RouteData.Values);
            string urlFormat = dict["action"].ToString().Replace("~/Views", "").Replace(".cshtml", "") + "?pageIndex={0}";
            Hashtable style = new Hashtable();
            style.Add("homePage", "home_pages");
            style.Add("prePage", "up_pages");
            style.Add("nextPage", "up_pages");
            style.Add("lastPage", "home_pages");
            style.Add("currentPage", "ture_box");
            style.Add("text", "input text_d");
            style.Add("button", "button_par enter_a");
            string str = PagerBar(helper, style, urlFormat, false, pageIndex, pageSize, recordCount, mode);
            HtmlString htmlStr = new HtmlString(str);
            return htmlStr;
        }
    }

    /// <summary>
    /// 分页模式
    /// </summary> 
    public enum PageMode
    {
        /// <summary>
        /// 普通分页模式
        /// </summary>
        Normal,
        /// <summary>
        /// 普通分页加数字分页
        /// </summary>
        Numeric
    }


    public class PagerHelpCustomer
    {
        public static int GetPageSize()
        {
            int pageSize = 20;

            string tempSize = HttpContext.Current.Request.Form.Get("PageSize");
            if (tempSize == null)
            {
                tempSize = HttpContext.Current.Request["PageSize"];
            }
            if (!String.IsNullOrEmpty(tempSize) && tempSize != "0")
            {
                int.TryParse(tempSize, out pageSize);
                HttpContext.Current.Session["PageSize"] = pageSize;
            }
            else
            {
                int? sessionPageSize = HttpContext.Current.Session["PageSize"] as int?;
                if (sessionPageSize.HasValue)
                    pageSize = sessionPageSize.Value;

            }

            return pageSize == 0 ? 20 : pageSize;
        }
    }

}